<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item,index) in list"
        :key="index"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | relative}}</span>
            <van-button @click="replyShow(item)" size="mini" type="default">回复{{item.reply_count}}</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" @click="toggleLiking(item)" :name="item.is_liking? 'like' : 'like-o'" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        v-model.trim="content"
        placeholder="请输入评论内容"
      >
        <van-button @click="addComment" slot="button" size="mini" type="info">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->

    <!-- 回复评论组件 -->
    <van-popup v-model="isReplyShow" round position="bottom" :style="{ height: '80%' }">
      <comment-reply v-if="isReplyShow" @close='isReplyShow = false' :comment='currentComment' :article-id='articleId'></comment-reply>
    </van-popup>
  </div>
</template>

<script>
import CommentReply from './commentReply'
import { reqAddComments, reqGetComments, reqAddCommentsLike, reqDelCommentsLike } from '@/api/comment.js'
export default {
  name: 'ArticleComment',
  props: {
    articleId: {
      type: String,
      required: true
    }
  },
  components: {
    CommentReply
  },
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      content: '', // 输入框的内容
      offset: null, // 文章的偏移量，表示从下一页开始加载
      isReplyShow: false, // 评论回复组件关闭
      currentComment: {} // 记录当前谈论的对象
    }
  },

  methods: {
    // 刷新加载数据
    async onLoad () {
      // 1.发送请求
      const res = await reqGetComments(this.offset, this.articleId)
      // console.log(res)
      const arr = res.data.data.results
      // 2.将数据追加到新数据后
      this.list = [...this.list, ...arr]
      // 3.刷新完成，关闭loading
      this.loading = false
      // 4.判断数据是否全部加载完成，完成就将finished改成true
      if (arr.length === this.list.length) {
        this.finished = true
      }
    },
    // 发布评论
    async addComment () {
      try {
        const res = await reqAddComments(this.articleId, this.content)
        //   console.log(res.data)
        //   将发布的评论添加到列表最前面
        this.list.unshift(res.data.data.new_obj)
        // this.loading = false
        //   发布后清空输入框
        this.content = ''
        this.$toast.success('操作成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 切换是否点赞
    async toggleLiking (comment) {
      try {
        if (comment.is_liking) {
          await reqDelCommentsLike(comment.com_id.toString())
          this.$toast.success('已取赞')
        } else {
          await reqAddCommentsLike(comment.com_id.toString())
          this.$toast.success('已点赞')
        }
        comment.is_liking = !comment.is_liking
      } catch (err) {
        console.log(err)
      }
    },
    // 评论回复组件打开
    replyShow (comment) {
      // 展示弹窗
      this.isReplyShow = true
      // 更新正在回复的组件
      this.currentComment = comment
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
